<?php
/**
 * 价值链页面下载页
 */

require_once 'config/config.php';
require_once 'includes/db.php';

// 检查是否已安装
if (!file_exists(__DIR__ . '/config/installed.lock')) {
    header('Location: install.php');
    exit;
}

try {
    $db = new Database();
    $content = $db->getValueChainContent();
} catch (Exception $e) {
    $error = '加载页面内容失败: ' . $e->getMessage();
}

// 处理下载
if (isset($_POST['download']) || isset($_GET['download'])) {
    if ($content && $content['file_id']) {
        // 实际下载文件
        header('Location: download.php?id=' . $content['file_id']);
        exit;
    } else {
        showToast('未配置下载文件', 'error');
    }
}

function formatFileSize($sizeStr) {
    return $sizeStr ?: '未知大小';
}

function showToast($message, $type) {
    $_SESSION['toast_message'] = $message;
    $_SESSION['toast_type'] = $type;
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= $content['page_title'] ?? '价值链资源下载' ?></title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            background: #f5f5f7;
            min-height: 100vh;
            padding: 20px;
        }

        .container {
            max-width: 900px;
            margin: 0 auto;
        }

        .download-page {
            background: white;
            border-radius: 20px;
            padding: 60px 40px;
            box-shadow: 0 8px 40px rgba(0,0,0,0.08);
            margin-top: 40px;
        }

        .page-header {
            text-align: center;
            margin-bottom: 50px;
        }

        .page-icon {
            font-size: 80px;
            margin-bottom: 24px;
        }

        .page-title {
            font-size: 42px;
            font-weight: 700;
            color: #1d1d1f;
            margin-bottom: 16px;
        }

        .page-description {
            font-size: 18px;
            color: #86868b;
            line-height: 1.6;
            max-width: 600px;
            margin: 0 auto;
        }

        .file-info {
            background: #f5f5f7;
            border-radius: 16px;
            padding: 32px;
            margin-bottom: 40px;
        }

        .file-header {
            display: flex;
            align-items: center;
            gap: 20px;
            margin-bottom: 24px;
        }

        .file-icon {
            font-size: 64px;
        }

        .file-details h2 {
            font-size: 28px;
            font-weight: 600;
            color: #1d1d1f;
            margin-bottom: 8px;
        }

        .file-meta {
            display: flex;
            gap: 24px;
            color: #86868b;
            font-size: 14px;
        }

        .file-meta-item {
            display: flex;
            align-items: center;
            gap: 6px;
        }

        .file-description {
            color: #515154;
            font-size: 16px;
            line-height: 1.8;
            padding-top: 24px;
            border-top: 1px solid #d2d2d7;
        }

        .download-section {
            margin-top: 40px;
        }

        .download-button {
            width: 100%;
            padding: 20px 40px;
            background: #007AFF;
            color: white;
            border: none;
            border-radius: 12px;
            font-size: 18px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 12px;
            box-shadow: 0 4px 20px rgba(0, 122, 255, 0.3);
            text-decoration: none;
        }

        .download-button:hover {
            background: #0056CC;
            transform: translateY(-2px);
            box-shadow: 0 6px 30px rgba(0, 122, 255, 0.4);
        }

        .download-info {
            text-align: center;
            margin-top: 16px;
            color: #86868b;
            font-size: 14px;
        }

        .stats-bar {
            display: flex;
            justify-content: center;
            gap: 40px;
            margin-top: 30px;
            padding-top: 30px;
            border-top: 1px solid #d2d2d7;
        }

        .stat-item {
            text-align: center;
        }

        .stat-value {
            font-size: 24px;
            font-weight: 700;
            color: #1d1d1f;
            margin-bottom: 4px;
        }

        .stat-label {
            font-size: 14px;
            color: #86868b;
        }

        .toast {
            position: fixed;
            top: 20px;
            right: 20px;
            padding: 16px 24px;
            border-radius: 10px;
            color: white;
            font-weight: 500;
            box-shadow: 0 4px 20px rgba(0,0,0,0.15);
            z-index: 1000;
            transform: translateX(400px);
            transition: transform 0.3s;
        }

        .toast.show {
            transform: translateX(0);
        }

        .toast.success {
            background: #34C759;
        }

        .toast.error {
            background: #FF3B30;
        }

        .empty-state {
            text-align: center;
            padding: 60px 20px;
            color: #86868b;
        }

        .empty-state-icon {
            font-size: 80px;
            margin-bottom: 20px;
        }

        .empty-state-text {
            font-size: 18px;
        }

        .admin-link {
            position: fixed;
            bottom: 20px;
            right: 20px;
            padding: 12px 24px;
            background: rgba(0, 122, 255, 0.1);
            color: #007AFF;
            border-radius: 20px;
            text-decoration: none;
            font-size: 14px;
            font-weight: 500;
            transition: all 0.3s;
        }

        .admin-link:hover {
            background: rgba(0, 122, 255, 0.2);
        }

        @media (max-width: 768px) {
            .download-page {
                padding: 40px 24px;
                margin-top: 20px;
            }

            .page-title {
                font-size: 32px;
            }

            .file-header {
                flex-direction: column;
                text-align: center;
            }

            .file-details h2 {
                font-size: 22px;
            }

            .stats-bar {
                flex-direction: column;
                gap: 20px;
            }
        }
    </style>
</head>
<body>
    <?php if (isset($_SESSION['toast_message'])): ?>
        <div class="toast <?= $_SESSION['toast_type'] ?>" id="toast">
            <?= $_SESSION['toast_message'] ?>
        </div>
        <?php unset($_SESSION['toast_message'], $_SESSION['toast_type']); ?>
        <script>
            document.getElementById('toast').classList.add('show');
            setTimeout(() => {
                document.getElementById('toast').classList.remove('show');
            }, 3000);
        </script>
    <?php endif; ?>

    <div class="container">
        <?php if (!$content): ?>
            <div class="download-page">
                <div class="empty-state">
                    <div class="empty-state-icon">📄</div>
                    <div class="empty-state-text">暂无价值链内容，请联系管理员</div>
                </div>
            </div>
        <?php else: ?>
            <div class="download-page">
                <div class="page-header">
                    <div class="page-icon">🔗</div>
                    <h1 class="page-title"><?= htmlspecialchars($content['page_title']) ?></h1>
                    <p class="page-description"><?= nl2br(htmlspecialchars($content['page_description'])) ?></p>
                </div>

                <div class="file-info">
                    <div class="file-header">
                        <div class="file-icon">📦</div>
                        <div class="file-details">
                            <h2><?= htmlspecialchars($content['file_name']) ?></h2>
                            <div class="file-meta">
                                <div class="file-meta-item">
                                    <span>📏</span>
                                    <span><?= formatFileSize($content['file_size']) ?></span>
                                </div>
                                <div class="file-meta-item">
                                    <span>📅</span>
                                    <span><?= date('Y-m-d', strtotime($content['updated_at'])) ?></span>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="file-description">
                        <?= nl2br(htmlspecialchars($content['file_description'])) ?>
                    </div>
                </div>

                <div class="download-section">
                    <form method="POST">
                        <button type="submit" name="download" class="download-button">
                            <span style="font-size: 24px;">⬇️</span>
                            <span>立即下载</span>
                        </button>
                    </form>
                    <div class="download-info">
                        点击下载按钮即可获取文件
                    </div>
                </div>

                <div class="stats-bar">
                    <div class="stat-item">
                        <div class="stat-value"><?= $content['download_count'] ?></div>
                        <div class="stat-label">下载次数</div>
                    </div>
                    <div class="stat-item">
                        <div class="stat-value">100%</div>
                        <div class="stat-label">安全可靠</div>
                    </div>
                </div>
            </div>
        <?php endif; ?>
    </div>

    <a href="index.php" class="admin-link">管理后台</a>
</body>
</html>
